// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ByFolderViewTemplateSelector.cs" company="Mar3ek">
//   This code is licensed under the Mozilla Public License 2.0.
//   You can find the full license text at the following address:
//   https://winkee.codeplex.com/license
// </copyright>
// <summary>
//   Defines the ByFolderViewTemplateSelector type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WinKee.Common
{
    using Windows.Storage;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    using WinKee.Models;

    /// <summary>
    /// The data template selector for the folder navigation mode.
    /// </summary>
    public class ByFolderViewTemplateSelector : DataTemplateSelector
    {
        protected override DataTemplate SelectTemplateCore(object item)
        {
            if (item is PasswordGroup)
            {
                return (DataTemplate)Application.Current.Resources["FolderTemplate"];
            }

            var entry = item as PasswordEntry;
            if (entry == null)
            {
                return base.SelectTemplateCore(item);
            }

            var colorSetting = ApplicationData.Current.LocalSettings.ReadSetting(
                Definitions.UseEntryColorsSettingKey,
                false);

            var displayUsername =
                ApplicationData.Current.LocalSettings.ReadSetting(Definitions.DisplayUsernamesInListSettingKey, false);

            if (colorSetting)
            {
                if (displayUsername)
                {
                    return (DataTemplate)Application.Current.Resources["EntryTemplateWithUsernameAndColor"];
                }
                else
                {
                    return (DataTemplate)Application.Current.Resources["SimpleEntryTemplateWithColor"];
                }
            }
            else
            {
                if (displayUsername)
                {
                    return (DataTemplate)Application.Current.Resources["EntryTemplateWithUsername"];
                }
                else
                {
                    return (DataTemplate)Application.Current.Resources["SimpleEntryTemplate"];
                }
            }
        }
    }
}